﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using VAdvantage.DataBase;


namespace VAdvantageSvc
{

    [DataContract]
    public class AttachmentInfo
    {
        [DataMember]
        public string FileName { get; set; }
        [DataMember]
        public long Size { get; set; }
        [DataMember]
        public string StoredFileName { get; set; }
        [DataMember]
        public Byte[] Data
        {
            get;
            set;
        }



        [DataMember]
        public String TextMessage
        {
            get;
            set;
        }

        [DataMember]
        public List<AttachmentEntry> AttachmentEntries
        { get; set; }


        public static AttachmentInfo GetEntries(String msg, string title, byte[] data)
        {
            AttachmentInfo info = new AttachmentInfo();
            info.AttachmentEntries = new List<AttachmentEntry>();

            if (msg != null)
            {
                info.TextMessage = msg;
            }


            if (data == null)
                return null;

            if (data.Length == 0)
                return null;

            //	Old Format - single file
            if (!"zip".Equals(title))
            {
                info.AttachmentEntries.Add(new AttachmentEntry(title, data, 1));
                return info;
            }
            // convert byte[] to byte[] data
            byte[] sdata = ConvertTobyte(data);
            try
            {
                List<KeyValuePair<String, byte[]>> lst = DB.GetZipEntries(sdata);

                foreach (KeyValuePair<String, Byte[]> itm in lst)
                {
                    info.AttachmentEntries.Add(new AttachmentEntry(itm.Key, itm.Value, info.AttachmentEntries.Count + 1));
                }
            }
            catch (Exception ex)
            {
                //log.Log(Level.SEVERE, "loadLOBData", ex);
                //_items = null;
                return null;
            }
            return info;
        }



        public static byte[] ConvertTobyte(byte[] byteVar)
        {
            if (byteVar != null)
            {
                //int len = byteVar.Length;
                //byte[] byteData = new byte[len];
                //for (int i = 0; i < len; i++)
                //{
                //    byteData[i] = (byte)byteVar[i];
                //}
                byte[] byteData = new byte[byteVar.Length];
                System.Buffer.BlockCopy(byteVar, 0, byteData, 0, byteVar.Length);

                return byteData;
            }
            else
            {
                return null;
            }
        }
    }

    [DataContract]
    public class AttachmentEntry
    {
        [DataMember]
        public String Name
        {
            get;
            set;
        }

        [DataMember]
        public byte[] Data
        {
            get;
            set;
        }

        [DataMember]
        public int Index
        {
            get;
            set;
        }

        public AttachmentEntry(String name, byte[] data, int index)
        {
            Name = name;
            Data = data;
            Index = index;
        }

    }
    
}
